﻿using QLKDS.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QLKDS.DAL
{
    public class EmployeeLanguageManager
    {
        public EmployeeLanguageInfo GetEmployeeLanguageById(int Id)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryEmployeeLanguage = from d in db.EmployeeLanguages
                                   where d.Id.Equals(Id)
                                   select new EmployeeLanguageInfo { Id = d.Id, Description = d.Description, EmployeeId=d.EmployeeId,PlaceIssue=d.PlaceIssue,DateIssue=d.DateIssue,LanguageLevel=d.LanguageLevel,LanguageId=d.LanguageId};

                return queryEmployeeLanguage.First();
            }

        }

        public IList<EmployeeLanguageInfo> ListEmployeeLanguage()
        {
            using (var db = new QLKDSDataContext())
            {
                var queryEmployeeLanguage = from d in db.EmployeeLanguages
                                         join c in db.Languages
                                         on d.LanguageId equals c.Id
                                         join e in db.Employees
                                         on d.EmployeeId equals e.Id
                                         select new EmployeeLanguageInfo { Id = d.Id, Description = d.Description, EmployeeId = d.EmployeeId, PlaceIssue = d.PlaceIssue, DateIssue = d.DateIssue, LanguageLevel = d.LanguageLevel, LanguageId = d.LanguageId,LanguageName=c.Name,EmployeeName=e.Fullname };

                return queryEmployeeLanguage.ToList();
            }
        }


        public void CreateEmployeeLanguage(EmployeeLanguage employeeLanguage)
        {
            using (var db = new QLKDSDataContext())
            {
                db.EmployeeLanguages.InsertOnSubmit(employeeLanguage);

                db.SubmitChanges();

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="employeeLanguage"></param>
        public void UpdateEmployeeLanguage(EmployeeLanguage employeeLanguage)
        {
            using (var db = new QLKDSDataContext())
            {
                var _employeeLanguage = db.EmployeeLanguages.Single(d => d.Id.Equals(employeeLanguage.Id));

                _employeeLanguage.LanguageId = employeeLanguage.LanguageId;
                _employeeLanguage.PlaceIssue = employeeLanguage.PlaceIssue;
                _employeeLanguage.LanguageLevel = employeeLanguage.LanguageLevel;
                _employeeLanguage.DateIssue = employeeLanguage.DateIssue;
                _employeeLanguage.Description = employeeLanguage.Description;
              
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteEmployeeLanguage(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _employeeLanguage = db.EmployeeLanguages.Single(d => d.Id.Equals(id));

                db.EmployeeLanguages.DeleteOnSubmit(_employeeLanguage);
                db.SubmitChanges();
            }
        }

    }
}
